<?php 
////////////////////////
///Connect To Server////
////////////////////////
$host="localhost"; // Host name
$username="z247s504_admin"; // Mysql username
$password="PxLpCd07"; // Mysql password
$database="z247s504_website"; // Mysql Database

$con = mysql_connect($host,$username,$password);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
  
mysql_select_db($database, $con);
////////////////////////
///End Of Connection////
////////////////////////


////////////////////////
//Record Users Search///
////////////////////////
$currentDate = date('y,m,d'); //Gets the current Date
$ipAddress = $_SERVER['REMOTE_ADDR']; //Gets the users IP address

mysql_query("INSERT INTO searchHistory 
	(
	searchTerm,
	searchDate,
	clientIP
	) 

VALUES 
	(
	'$searchTerm',
	'$currentDate',
	'$ipAddress'
	)
")
////////////////////////
///////End Record///////
////////////////////////

?>

<div id="content">

<?php  
  
////////////////////////
//Search Site PHP Code//
////////////////////////

//SEARCHTERM = The ID of the search bar field

$searchTerm = $_POST["searchTerm"]; //Get the string the user entered

$searchTerm = strtolower($searchTerm); //Converts the searched string into lowercase
   
$searchTermArray = explode(' ',$searchTerm); //Converts each searched word into an array value

$searchTermArray = array_unique($searchTermArray);

$searchResults = array(); //Creates an array called searchResults

foreach ($searchTermArray as &$value) //For each value in the array preform the below
{
	
	//Add all the products that have a match in the product name
	$resultName = mysql_query("SELECT * FROM products WHERE keywords OR productName LIKE '%$value%'");

	while($row = mysql_fetch_array($resultName))

  	{
	
     	if ($row['onTrial'] == "Yes") {
		$onTrial = '<p id="saddleOnTrial">On Trial</p>';
		} else
		{
		$onTrial = '';
		}
		
		//Create the html to display on the webpage
		$searchDetailName = 
		'<form action="productDetails.php" method="post" name="productDetails">
		<input name="productID" type="hidden" value="'.$row['productID'].'" />
 		<div id="productContainer">
		<h3>'.$row['productName'].'</h3>
		<p id="price">£'.$row['productCost'].'</p>
    	<p id="productDetailsSmall">'.$row['productDescription'].'</p>
    	<input id="viewButton" name="submit" type="submit" value="View Saddle" />
    	<img src="images/saddles/'.$row['productID'].'_1.jpg" width="500" height="438" />
		'.$onTrial.'
		</div>
		</form>';
		
		
		array_push($searchResults,"$searchDetailName"); //Add the product name found into the searchResults array
			
    }
	
}

$countSearchResults = array_count_values($searchResults); //Counts the number of times the array value is in the array
//Counts the number of times the searched word appears in hte product name

$finalResults = array(); //Create an array called FINALRESULTS

foreach($countSearchResults as $key => $value) //Create array containting | COUNT & PRODUCT NAME

{
	
    //Add the value (Count) and (product name) into the COUNTSEARCHRESULTS array
	//Value = COUNT
	//Key = PRODUCT NAME
    
	//See the matched count remove //
	//array_push($finalResults,"<i class='hidden'>".$value."</i>".$key." ".$value." Matches");
	
	array_push($finalResults,"<i class='hidden'>".$value."</i>".$key);


}

arsort($finalResults); //Sort the array Highest to Lowest | COUNT value

$availableResults = count($finalResults); //Counts how many results have been found | how many values are in FINALRESULTS array

//If there are 0 values in hte FINALRESULTS array then:
if ($availableResults == 0)

   //Display - Sorry No Results Message
   {echo "<h3>Sorry No Results Found For <b>".$searchTerm."</b></h3>";}
   
  		else
   
   //Display - Results Message
   {echo "<h3>Search Results For <b>".$searchTerm."</b></h3>";}

//Display all the reults in the FINALRESULTS array
foreach($finalResults as $key => $value)
{
  //Print out the arrays values | SearchDetailsDescription & SearchDetailsName
  echo $value;
}
 
////////////////////////
//Search Site PHP Code//
////////////////////////  
?>